<?php

// +-----------------------------------------------------
// | Name: 星空框架 广告管理
// +-----------------------------------------------------
// | Version: V5.0
// +-----------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-----------------------------------------------------
// | Time: 2021-04-21 18:48
// +-----------------------------------------------------
declare(strict_types=1);

namespace app\xingkong\controller\v1;

use app\common\common\BackendController;
use app\common\model\Banner as BannerModel;
use app\common\model\BannerType as BannerTypeModel;
use app\common\model\PosterType as PosterTypeModel;
use app\common\model\Poster as PosterModel;
use think\facade\Db;
use think\facade\View;

/**
 * @xk 广告管理
 */
class Banner extends BackendController
{
    // +----------
    // | 广告管理
    // +--------------

    /**
     * @xk 广告管理
     *
     * @method index
     *
     * @param BannerModel $bannerModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T19:08:08+080
     */
    public function index(BannerModel $bannerModel)
    {
        $param = request()->param();
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int) $param['page'] : 1;
            $limit = isset($param['limit']) ? (int) $param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $condition = [];
            if ('' != $keywords) {
                $condition[] = ['b.banner_name|b.linkurl', 'like', '%'.$keywords.'%'];
            }
            $list = $bannerModel
          ->alias('b')
          ->leftJoin('bannerType bt', 'bt.banner_type_id=b.banner_type_id')
          ->where($condition)
          ->field('b.`banner_id`,b.`banner_name`,b.`linkurl`,b.show_type,b.`fileurl`,bt.`banner_type_name`,b.`order_id`,b.`status`,b.`show_pic_url`,b.`update_time`')
          ->withAttr('show_type', function ($value, $data) {
              return 1 == $value ? '图片' : '视频';
          })
          ->withAttr('fileurl', function ($value, $data) {
              if (2 == $data['show_type']) {
                  return getShowUrl($data['show_pic_url']);
              } else {
                  return getShowUrl($value);
              }
          })
          ->withAttr('show_pic_url', function ($value, $data) {
              if ($value) {
                  return getShowUrl($value);
              }
          })
          ->page($page, $limit)
          ->select()
          ->toArray();
            $count = $bannerModel->alias('b')->where($condition)->count();
            $code = 200;
            $msg = '请求成功';
            $result_code = 200;
            $result_msg = '加载成功';
            $resultData = [];
            $resultData['list'] = $list;
            $resultData['count'] = $count;
            $this->result($code, $msg, $result_code, $result_msg, $resultData);
        }

        return View::fetch();
    }

    /**
     * @xk 广告添加
     *
     * @method create
     *
     * @param BannerTypeModel $bannerTypeModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T19:13:09+080
     */
    public function create(BannerTypeModel $bannerTypeModel)
    {
        $list = $bannerTypeModel
                ->where([['status', '=', 1]])
                ->field('`banner_type_id`,`banner_type_name`')
                ->select()
                ->toArray();

        return View::fetch('', ['banner_type_list' => $list]);
    }

    /**
     * @xk 广告编辑
     *
     * @method edit
     *
     * @param BannerTypeModel $bannerTypeModel [description]
     * @param BannerModel     $bannerModel     [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T19:16:24+080
     */
    public function bannerEdit(BannerTypeModel $bannerTypeModel, BannerModel $bannerModel)
    {
        $param = request()->param();
        $banner_id = isset($param['banner_ids']) ? $param['banner_ids'] : 0;
        if ($banner_id <= 0) {
            $this->error('请选择需要编辑的广告');
        }
        $list = $bannerTypeModel->where([['status', '=', 1]])->field('`banner_type_id`,`banner_type_name`')->select();
        $info = $bannerModel
                ->field('*,fileurl as show_fileurl,show_pic_url as show_show_pic_url')
                ->withAttr('show_fileurl', function ($value, $data) {
                    if ($value) {
                        return getShowUrl($value);
                    }
                })
                ->withAttr('show_show_pic_url', function ($value, $data) {
                    if ($value) {
                        return getShowUrl($value);
                    }
                })
                ->find($banner_id);

        return View::fetch('', ['info' => $info, 'banner_type_list' => $list]);
    }

    /**
     * @xk 广告保存
     *
     * @method save
     *
     * @param BannerModel $bannerModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T19:17:50+080
     */
    public function save(BannerModel $bannerModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $act = isset($param['act']) ? $param['act'] : 'create';
            $check_result = $bannerModel->validate($param, $act);
            switch ($act) {
          case 'create':
            $res = $bannerModel->create($param);
            break;
          case 'edit':
            $res = $bannerModel->find($param['banner_id'])->save($param);
            break;
        }
            Db::commit();
            $result_code = 200;
            $result_msg = '加载成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }

    /**
     * @xk 动态修改广告
     *
     * @method chanageValue
     *
     * @param BannerModel $bannerModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T19:22:44+080
     */
    public function chanageValue(BannerModel $bannerModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $by_key = isset($param['by_key']) ? $param['by_key'] : '';
            $i = 0;
            $j = 0;
            if (!$by_key) {
                throw new \think\Exception('请选择需要修改的数据', 404);
            }
            $by_key_arr = explode(',', $by_key);
            foreach ($by_key_arr as $key => $value) {
                $newarr = [];
                $newarr['by_key'] = $value;
                $newarr['key'] = $param['key'];
                $newarr['value'] = $param['value'];
                $res = $bannerModel->chanageValue($newarr);
                if ($res) {
                    ++$i;
                }
            }
            $result_code = 200;
            $result_msg = '处理成功';
            Db::commit();
        } catch (\Exception $e) {
            $result_code = 404;
            $result_msg = $e->getMessage();
            Db::rollback();
        }

        $this->result($code, $msg, $result_code, $result_msg);
    }

    /**
     * @xk 广告删除
     *
     * @method destroy
     *
     * @param BannerModel $bannerModel [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T19:23:38+080
     */
    public function destroy(BannerModel $bannerModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $id_str = isset($param['id_str']) ? $param['id_str'] : '';
            $i = 0;
            $j = 0;
            if (!$id_str) {
                throw new \think\Exception('请选择需要删除的数据', 404);
            }
            $id_arr = explode(',', $id_str);
            foreach ($id_arr as $key => $value) {
                $res = $bannerModel->destroy($value);
                ++$i;
            }
            Db::commit();
            $result_code = 200;
            $result_msg = '处理成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
    // *****************
    // | 海报管理
    // *******************
    /**
     * @xk 广告管理
     *
     * @method poster
     * @param  PosterModel $posterModel               [description]
     * @return [type]      [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe    <zhonghuaxinxing@sina.cn>
     */
    public function poster(PosterModel $posterModel)
    {
        $param = request()->param();
        if (request()->isAjax()) {
            $page = isset($param['page']) ? (int) $param['page'] : 1;
            $limit = isset($param['limit']) ? (int) $param['limit'] : 30;
            $keywords = isset($param['keywords']) ? $param['keywords'] : '';
            $condition = [];
            if ('' != $keywords) {
                $condition[] = ['p.poster_name', 'like', '%'.$keywords.'%'];
            }
            $list = $posterModel
                  ->alias('p')
                  ->leftJoin('posterType pt', 'pt.poster_type_id=p.poster_type_id')
                  ->where($condition)
                  ->field('p.`poster_id`,p.`poster_name`,p.show_postion,pt.`poster_type_name`,p.`order_id`,p.show_type,p.`status`,p.`back_ground_picurl`,p.`update_time`')
                  ->withAttr('show_type', function ($value, $data) {
                      return 1 == $value ? '图片' : '视频';
                  })
                  ->withAttr('back_ground_picurl', function ($value, $data) {
                      return getShowUrl($value);
                  })
                  ->withAttr('show_postion',function($value,$data){
                    switch ($value) {
                      case '1':
                        return  '左上';
                        break;
                      case '2':
                        return '右上';
                        break;
                      case '3':
                        return '左中';
                        break;
                      case '4':
                        return '右中';
                        break;
                      case '5':
                        return '左下';
                        break;
                      case '6':
                        return '右下';
                        break;
                    }
                  })
                  ->page($page, $limit)
                  ->select()
                  ->toArray();
            $count = $posterModel->alias('p')->where($condition)->count();
            $code = 200;
            $msg = '请求成功';
            $result_code = 200;
            $result_msg = '加载成功';
            $resultData = [];
            $resultData['list'] = $list;
            $resultData['count'] = $count;
            $this->result($code, $msg, $result_code, $result_msg, $resultData);
        }

        return View::fetch();
    }

    /**
     * @xk 海报添加
     *
     * @method posterCreate
     * @param  PosterTypeModel  $posterTypeModel               [description]
     * @return [type]       [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe     <zhonghuaxinxing@sina.cn>
     */
    public function posterCreate(PosterTypeModel $posterTypeModel)
    {
        $list = $posterTypeModel
                ->where([['status', '=', 1]])
                ->field('`poster_type_id`,`poster_type_name`')
                ->select()
                ->toArray();

        return View::fetch('', ['poster_type_list' => $list]);
    }

    /**
     * @xk 海报编辑
     *
     * @method posterEdit
     * @param  PosterTypeModel $posterTypeModel           [description]
     * @param  PosterModel     $posterModel               [description]
     * @return [type]          [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe        <zhonghuaxinxing@sina.cn>
     */
    public function posterEdit(PosterTypeModel $posterTypeModel, PosterModel $posterModel)
    {
        $param = request()->param();
        $poster_id = isset($param['poster_id']) ? $param['poster_id'] : 0;
        if ($poster_id <= 0) {
            $this->error('请选择需要编辑的海报');
        }
        $list = $posterTypeModel
                ->where([['status', '=', 1]])
                ->field('`poster_type_id`,`poster_type_name`')
                ->select()
                ->toArray();
        $info = $posterModel
                ->field('*,back_ground_picurl as show_back_ground_picurl')
                ->withAttr('show_back_ground_picurl', function ($value, $data) {
                    if ($value) {
                        return getShowUrl($value);
                    }
                })
                ->find($poster_id);

        return View::fetch('', ['info' => $info, 'poster_type_list' => $list]);
    }

    /**
     * @xk 海报保存
     *
     * @method posterSave
     * @param  PosterModel $posterModel               [description]
     * @return [type]      [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe    <zhonghuaxinxing@sina.cn>
     */
    public function posterSave(PosterModel $posterModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $act = isset($param['act']) ? $param['act'] : 'create';
            $check_result = $posterModel->validate($param, $act);
            switch ($act) {
          case 'create':
            $res = $posterModel->create($param);
            break;
          case 'edit':
            $res = $posterModel->find($param['poster_id'])->save($param);
            break;
        }
            Db::commit();
            $result_code = 200;
            $result_msg = '加载成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }

    /**
     * @xk 动态修改海报
     *
     * @method posterChanageValue
     * @param  PosterModel        $posterModel               [description]
     * @return [type]             [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe           <zhonghuaxinxing@sina.cn>
     */
    public function posterChanageValue(PosterModel $posterModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $by_key = isset($param['by_key']) ? $param['by_key'] : '';
            $i = 0;
            $j = 0;
            if (!$by_key) {
                throw new \think\Exception('请选择需要修改的数据', 404);
            }
            $by_key_arr = explode(',', $by_key);
            foreach ($by_key_arr as $key => $value) {
                $newarr = [];
                $newarr['by_key'] = $value;
                $newarr['key'] = $param['key'];
                $newarr['value'] = $param['value'];
                $res = $posterModel->chanageValue($newarr);
                if ($res) {
                    ++$i;
                }
            }
            $result_code = 200;
            $result_msg = '处理成功';
            Db::commit();
        } catch (\Exception $e) {
            $result_code = 404;
            $result_msg = $e->getMessage();
            Db::rollback();
        }

        $this->result($code, $msg, $result_code, $result_msg);
    }

    /**
     * @xk 海报删除
     *
     * @method posterDestroy
     * @param  PosterModel   $posterModel               [description]
     * @return [type]        [description]
     * @Time   2021-10-19
     * @City   青岛
     * @Author xkyinzhe      <zhonghuaxinxing@sina.cn>
     */
    public function posterDestroy(PosterModel $posterModel)
    {
        Db::startTrans();
        $code = 200;
        $msg = '请求成功';
        try {
            $param = request()->param();
            $id_str = isset($param['id_str']) ? $param['id_str'] : '';
            $i = 0;
            $j = 0;
            if (!$id_str) {
                throw new \think\Exception('请选择需要删除的数据', 404);
            }
            $id_arr = explode(',', $id_str);
            foreach ($id_arr as $key => $value) {
                $res = $posterModel->destroy($value);
                ++$i;
            }
            Db::commit();
            $result_code = 200;
            $result_msg = '处理成功';
        } catch (\Exception $e) {
            Db::rollback();
            $result_code = 404;
            $result_msg = $e->getMessage();
        }
        $this->result($code, $msg, $result_code, $result_msg);
    }
}
